package com.yonyou.isv.demo.openapi.sso;

import com.yonyou.isv.demo.openapi.base.properties.OpenApiProperties;
import com.yonyou.isv.demo.openapi.base.module.reponse.ApiDataResponse;
import com.yonyou.isv.demo.openapi.api.BaseOpenApi;
import com.yonyou.isv.demo.openapi.base.properties.UCFCoreProperties;
import com.yonyou.isv.demo.openapi.pojo.ThirdUserBo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


/**
 * @description: 单点登录
 * @author: nishch
 * @create: 2021-07-07
 **/
@Component
public class SingleSignOn extends BaseOpenApi {

    //获取临时code
    private final static String sso_code = "/yonbip/yht/getThirdLoginCode";
    //单点登录拼接地址
    private final static String open_ys_url = "%s/cas/thirdOauth2CodeLogin?thirdUCId=%s&code=%s&service=%s";
    @Resource
    OpenApiProperties properties;
    @Resource
    UCFCoreProperties ucfCoreProperties;


    /**
     * 获取登录到控制台的地址
     * @param entity 第三方用户信息
     * @param isNode true单点至指定节点,false单点至控制台
     * @return 最终实现单点的地址
     * @throws Exception 获取免登code异常
     */
    public String getSSOUrlToIndex(ThirdUserBo entity,Boolean isNode) throws Exception {
        String code = getCode(entity);
        String thirdUCId = entity.getThirdUcId();
        StringBuilder service = new StringBuilder();
        if(isNode){
            String node = URLEncoder.encode(String.format(getAccess_tet_node(), entity.getServiceCode()), StandardCharsets.UTF_8.toString());
            String nodeUrl = getAccess_tet()+"?tenantId="+properties.getTenantId()+"&service="+node;
            service.append(URLEncoder.encode(nodeUrl,StandardCharsets.UTF_8.toString()));
        }else{
            service.append(URLEncoder.encode(getAccess_tet(),StandardCharsets.UTF_8.toString()));
        }
        return String.format(open_ys_url,ucfCoreProperties.getSsoYhtUrl(),thirdUCId,code,service);
    }


    /**
     * 获取登录到控制台的地址
     * @param entity 第三方用户信息
     * @param service 要跳转的真实地址
     * @return 最终实现单点的地址
     * @throws Exception 获取免登code异常
     */
    public String getSSOUrlToUrl(ThirdUserBo entity,String service) throws Exception {
        String code = getCode(entity);
        String thirdUCId = entity.getThirdUcId();
        String node = URLEncoder.encode(service, StandardCharsets.UTF_8.toString());
        String nodeUrl = getAccess_tet()+"?tenantId="+properties.getTenantId()+"&service="+node;
        return String.format(open_ys_url,ucfCoreProperties.getSsoYhtUrl(),thirdUCId,code,URLEncoder.encode(nodeUrl,StandardCharsets.UTF_8.toString()));
    }

    /**
     * 单点至控制台
     * @return String 目标路径
     */
    private String getAccess_tet() {
        return ucfCoreProperties.getSsoYsUrl()+ "/login";
    }



    /**
     * 单点至指定节点
     * @return String 目标路径
     */
    private String getAccess_tet_node() {
        return ucfCoreProperties.getSsoYsUrl()+ "/?effect=headless#/service/%s";
    }

    /**
     * 获取免登code
     * @param entity  第三方用户信息
     * @return code
     * @throws Exception  接口返回异常
     */
    private String getCode(ThirdUserBo entity) throws Exception {
        if(null==entity|| StringUtils.isBlank(entity.getThirdUcId()) || StringUtils.isBlank(entity.getUserId())){
            throw new Exception("用户参数不全");
        }
        ApiDataResponse apiDataResponse = postForEntity(sso_code,entity, ApiDataResponse.class);
        return getData(apiDataResponse).get("code").toString();
    }

}
